﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LastoRevival.Service.Implementations.Exceptions;
using LastoRevival.Service.Implementations.Data;
using LastoRevival.Service.Implementations.Settings;

namespace LastoRevival.Service.Implementations
{
	public abstract class ServiceBase
	{
		private IDataProvider DataProvider
		{
			get
			{
				return DependencyFactory.GetDataProvider();
			}
		}

		protected void UpdateSession(Guid loginToken)
		{
			if (loginToken == Guid.Empty)
			{
				// anonymous user, no sense updating a session that doesn't exist
				return;
			}

			var expiration = DataProvider.GetSessionExpiration(loginToken);

			// update the session if required (bump x minutes out, unless the session was set to always remember)
			var newExpiration = DateTime.UtcNow.AddMinutes(ServiceSettings.GetInstance().LoginMinutes);

			if (expiration < newExpiration)
			{
				DataProvider.RefreshUserSession(loginToken, newExpiration);
			}
		}

		protected void VerifyLoggedIn(Guid loginToken)
		{
			var expiration = DataProvider.GetSessionExpiration(loginToken);

			if (expiration < DateTime.UtcNow)
			{
				throw new SessionExpiredException();
			}
		}
	}
}
